﻿using System;
using System.Collections.Generic;
using BruteForce.Animation;
using BruteForce.Characters;
using BruteForce.GameObjects;
using BruteForce.Screens.PlayingScreen.Map;
using BruteForce.Screens.PlayingScreen.Panels;
using BruteForce.Utilities;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace BruteForce.NewManagers {
    public class PlayerManager : Manager {
        public Player HumanPlayer;
        public Player MonsterPlayer;
        public int MaxAllowedUsedResourcesInTurn;
        public int ResourcesAllowedLeftToUse;
        private Transition playerSwitchTransition = new LinearTransition(2);


        public MonitoredProperty<Player> CurrentPlayer = new MonitoredProperty<Player>(null);

        private static PlayerManager _instance = new PlayerManager();


        private PlayerManager() {
            Reset();
        }

        public void Reset()
        {
            CurrentPlayer.Property = HumanPlayer;

            MaxAllowedUsedResourcesInTurn = 10;
            ResourcesAllowedLeftToUse = MaxAllowedUsedResourcesInTurn;
        }

        public static PlayerManager Instance { get { return _instance; } }

       
//        public void Explode(Bomber bomber) {
//            foreach (var mapCell in TileMap.HighlightBackUp) {
//                Character characterBeenShootAt = mapCell.CharacterInCell;
//                if (characterBeenShootAt != null) {
////                    if (characterBeenShootAt.BelongsToPlayer != bomber.BelongsToPlayer)
//                        characterBeenShootAt.DecreaseLife(bomber.CurrentResources.Power);
//                    if (characterBeenShootAt.InitialResources.Life <= 0)
//                    {
//                        if (characterBeenShootAt.BelongsToPlayer == PlayerType.Human)
//                            HumanPlayer.RemoveGamePiece(characterBeenShootAt);
//                        else
//                            MonsterPlayer.RemoveGamePiece(characterBeenShootAt);
//                    }
//                }
//            }

//        }

        public void AddPlayer(Player player, PlayerType playerType) {
            switch (playerType) {
                case PlayerType.Human:
                    HumanPlayer = player;
                    CurrentPlayer.Property = HumanPlayer;
                    break;
                case PlayerType.Monster:
                    MonsterPlayer = player;
                    break;
            }
        }

        public override void Draw(GameTime gameTime, BFGame game) {
            if (playerSwitchTransition.Running)
            {
                SpriteBatch SpriteBatch = BFGame.SpriteBatch;
                SpriteBatch.End();
                SpriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.NonPremultiplied);

                Vector2 loc = new Vector2(400, 250);
                Console.WriteLine("" + ((1.0 - playerSwitchTransition.Value)));
                Color c = new Color(35, 35, 35, (int)(255 * (1f - (float)playerSwitchTransition.Value)));
                //, 255f*(1f - (float) playerSwitchTransition.Value)
                BFGame.SpriteBatch.DrawString(BFGame.MessageFont, (CurrentPlayer.Property.PlayerType == PlayerType.Human? "Humans": "Monsters") + " Turn", loc, c);
            }
        }

        protected override void _Update(GameTime gameTime, BFGame game) {

//            checkGameEnded();
        }

//        private Player checkGameEnded()
//        {
//            if (HumanPlayer.Characters.Count == 0 && HumanPlayer.Characters.Count != 0)
//            {
//                
//            }
//        }

        public void SwitchTurns() {
            playerSwitchTransition.StartTransition();
            if (CurrentPlayer.Property.PlayerType == PlayerType.Human) {
                CurrentPlayer.Property = MonsterPlayer;

                MonsterPlayer.InitTurn();
            } else {
                HumanPlayer.InitTurn();
                CurrentPlayer.Property = HumanPlayer;
            }

            GamePanel.Current.SelectNextCharacter();
        }


        public override void LoadContent(ContentManager content) {

        }

        public override void Initialize() {

        }

        public Player GetPlayer(PlayerType playerType) {
            switch (playerType) {
                case PlayerType.Human:
                    return HumanPlayer;
                case PlayerType.Monster:
                    return MonsterPlayer;
            }

            return CurrentPlayer.Property;
        }
    }
}